/**
 * 
 */
package com.carnation.qa.socketserver.proxy.moi;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.carnation.mong.platform.service.MOISession;
import com.carnation.qa.socketserver.domain.Item;
import com.carnation.qa.socketserver.proxy.Builder;

/**
 * @author Nick
 * 
 */
public class MOISessionImpl extends AbstractMOIServiceImpl {

	private final Log log = LogFactory.getLog(getClass());

	private MOISession service;

	public MOISessionImpl(List<Item> items) {
		super(items);
		service = ServiceFactory.getService(MOISession.class);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.carnation.qa.socketserver.proxy.AbstractServiceImpl#invoke(com.carnation
	 * .qa.socketserver.domain.Item)
	 */
	@Override
	protected String invoke() {
		Item item = getItems().get(0);
		String method = getMethodName(item);
		String returnStr;
		if ("getAllDataPrivilegesOfUser".equals(method)) {
			returnStr = getAllDataPrivilegesOfUser(item);
		} else if ("getAllPrivilegesOfUser".equals(method)) {
			returnStr = getAllPrivilegesOfUser(item);
		} else if ("getAllRolesOfUser".equals(method)) {
			returnStr = getAllRolesOfUser(item);
		} else if ("getDataPrivilege".equals(method)) {
			returnStr = getDataPrivilege(item);
		} else if ("getGroupsOfUser".equals(method)) {
			returnStr = getGroupsOfUser(item);
		} else if ("getRolesOfUser".equals(method)) {
			returnStr = getRolesOfUser(item);
		} else if ("getSessionId".equals(method)) {
			returnStr = getSessionId(item);
		} else if ("getUser".equals(method)) {
			returnStr = getUser(item);
		} else if ("hasPrivilege".equals(method)) {
			returnStr = hasPrivilege(item);
		} else if ("isAdmin".equals(method)) {
			returnStr = isAdmin(item);
		} else if ("isValid".equals(method)) {
			returnStr = isValid(item);
		} else {
			throw new IllegalArgumentException(method);
		}
		return returnStr;
	}

	private String isValid(Item item) {
		if (log.isDebugEnabled()) {
			log.debug("isValid");
		}
		try {
			Object obj = service.isValid();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String isAdmin(Item item) {
		if (log.isDebugEnabled()) {
			log.debug("isAdmin");
		}
		try {
			Object obj = service.isAdmin();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String hasPrivilege(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			String privilege = getString(item.getChildren().get(0)
					.getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("hasPrivilege privilege:" + privilege);
			}
			try {
				Object obj = service.hasPrivilege(privilege);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getUser(Item item) {
		if (log.isDebugEnabled()) {
			log.debug("getUser");
		}
		try {
			Object obj = service.getUser();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getSessionId(Item item) {
		if (log.isDebugEnabled()) {
			log.debug("getSessionId");
		}
		try {
			Object obj = service.getSessionId();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getRolesOfUser(Item item) {
		if (log.isDebugEnabled()) {
			log.debug("getRolesOfUser");
		}
		try {
			Object obj = service.getRolesOfUser();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getGroupsOfUser(Item item) {
		if (log.isDebugEnabled()) {
			log.debug("getGroupsOfUser");
		}
		try {
			Object obj = service.getGroupsOfUser();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getDataPrivilege(Item item) {
		if (item.getChildren().size() == 1) {// 参数个数
			String dataId = getString(item.getChildren().get(0).getLabelParts()[1]);
			if (log.isDebugEnabled()) {
				log.debug("getDataPrivilege dataId:" + dataId);
			}
			try {
				Object obj = service.getDataPrivilege(dataId);
				if (log.isDebugEnabled()) {
					log.debug(obj);
				}
				return new Builder(obj, null, item.getTabSeparator())
						.toString();
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(item.toString(), e);
				}
				return e.toString();
			}
		}
		throw new IllegalArgumentException(item.toString());
	}

	private String getAllRolesOfUser(Item item) {
		if (log.isDebugEnabled()) {
			log.debug("getAllRolesOfUser");
		}
		try {
			Object obj = service.getAllRolesOfUser();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getAllPrivilegesOfUser(Item item) {
		if (log.isDebugEnabled()) {
			log.debug("getAllPrivilegesOfUser");
		}
		try {
			Object obj = service.getAllPrivilegesOfUser();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

	private String getAllDataPrivilegesOfUser(Item item) {
		if (log.isDebugEnabled()) {
			log.debug("getAllDataPrivilegesOfUser");
		}
		try {
			Object obj = service.getAllDataPrivilegesOfUser();
			if (log.isDebugEnabled()) {
				log.debug(obj);
			}
			return new Builder(obj, null, item.getTabSeparator()).toString();
		} catch (Exception e) {
			if (log.isErrorEnabled()) {
				log.error(item.toString(), e);
			}
			return e.toString();
		}
	}

}
